package com.fau.wm.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.Hibernate;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

/**
 * 入库详细表
 * @author liy
 * @since 2014年7月25日,上午9:35:31
 */
@Entity
@Table(name = "medical_storage_detail")
public class MedicalStorageDetail implements Serializable, JSONAware{

	private static final long serialVersionUID = -3620295004812353057L;
	
	private String id;
	private String storageId;				// 入库表id
	private String medicineAppendId;		// 药品附加信息id
	private Integer quantity;				// 数量
	private Date createTime = new Date();	// 创建时间
	private Date updateTime = new Date();	// 更新时间
	
	private MedicalStorage storage;			// 入库实体类
	private MedicalMedicine medicine;		// 药品实体类
	
	private String medicineId;				// 药品id
	private Date productionDate;			// 药品生产日期
	private Date validTo;					// 药品有效期至
	private String hospitalId;				// 医药企业id
	
	private String stype;					// 入库类型
	private String unit;					// 药品单位
	// 不关联数据库,用于查询
	private String serialNum;				// 入库单编号
	private String title;					// 标题
	private Date startTime;					// 入库开始时间
	private Date endTime;					// 入库截止时间
	private String warehouseName;           // 仓库库名称
	private String auditUser;				// 审核人
	private String medicineNo;				// 药品编号
	private String medicineName;            // 药品名称
	private String warehouseId;            	// 仓库Id
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Transient
	public String getStorageId() {
		return storageId;
	}

	public void setStorageId(String storageId) {
		this.storageId = storageId;
	}

	@Column(name = "medicine_append_id", length = 32)
	public String getMedicineAppendId() {
		return medicineAppendId;
	}

	public void setMedicineAppendId(String medicineAppendId) {
		this.medicineAppendId = medicineAppendId;
	}

	@Column(name = "quantity", length = 11)
	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	@Transient
	public String getWarehouseName() {
		return warehouseName;
	}
	
	public void setWarehouseName(String warehouseName) {
		this.warehouseName = warehouseName;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "medicine_id")
	public MedicalMedicine getMedicine() {
		return medicine;
	}

	public void setMedicine(MedicalMedicine medicine) {
		this.medicine = medicine;
	}
	
	@Transient
	public String getMedicineName() {
		return medicineName;
	}

	public void setMedicineName(String medicineName) {
		this.medicineName = medicineName;
	}

	@Transient
	public String getMedicineId() {
		return medicineId;
	}

	public void setMedicineId(String medicineId) {
		this.medicineId = medicineId;
	}

	@Transient
	public Date getProductionDate() {
		return productionDate;
	}

	public void setProductionDate(Date productionDate) {
		this.productionDate = productionDate;
	}

	@Transient
	public Date getValidTo() {
		return validTo;
	}

	public void setValidTo(Date validTo) {
		this.validTo = validTo;
	}
	
	@Transient
	public String getHospitalId() {
		return hospitalId;
	}

	public void setHospitalId(String hospitalId) {
		this.hospitalId = hospitalId;
	}

	@Transient
	public String getStype() {
		return stype;
	}

	public void setStype(String stype) {
		this.stype = stype;
	}

	@Transient
	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	@Transient
	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	@Transient
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Transient
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	@Transient
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@Transient
	public String getAuditUser() {
		return auditUser;
	}

	public void setAuditUser(String auditUser) {
		this.auditUser = auditUser;
	}

	@Transient
	public String getMedicineNo() {
		return medicineNo;
	}

	public void setMedicineNo(String medicineNo) {
		this.medicineNo = medicineNo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "storage_id")
	public MedicalStorage getStorage() {
		return storage;
	}

	public void setStorage(MedicalStorage storage) {
		this.storage = storage;
	}
	
	@Transient
	public String getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(String warehouseId) {
		this.warehouseId = warehouseId;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("quantity", quantity);
		obj.put("unit", unit);
		obj.put("name", medicine.getName());
		obj.put("medicineId", medicine.getId());
		obj.put("medicineNo", medicine.getSerialNum());
		obj.put("medicineUnit", medicine.getUnitValue());
		obj.put("createTime", DateFormatUtils.format(createTime, "yyyy-MM-dd HH:mm:ss"));
		if(null != productionDate){
			obj.put("productionDate", DateFormatUtils.format(productionDate, "yyyy-MM-dd"));
		}
		if(null != validTo){
			obj.put("validTo", DateFormatUtils.format(validTo, "yyyy-MM-dd"));
		}
		if(Hibernate.isInitialized(storage)){
			obj.put("storageId", storage.getId());
			obj.put("sserialNum", storage.getSerialNum());
			obj.put("stitle", storage.getTitle());
			obj.put("stype", stype);
			if(StringUtils.isNotBlank(storage.getAuditUser().getName())){
				obj.put("sauditUserName", storage.getAuditUser().getName());
			}
			obj.put("warehouseName", storage.getWarehouse().getName());
			obj.put("storageTime", DateFormatUtils.format(storage.getStorageTime(), "yyyy-MM-dd"));
		}
		return obj.toString();
	}
	
	
	
}
